﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;

namespace OpenWaves.ActivityFeed.Sharepoint
{
    public class ListEventRegistration
    {
        private const string ActivtyFeedEventName = "ActivityFeedEvent";

        public static void RegisterEventReceiver(SPList list, Type eventReceiverClass, SPEventReceiverType eventType)
        {
            var def = list.EventReceivers.Add();
            def.Assembly = eventReceiverClass.Assembly.FullName;
            def.Class = eventReceiverClass.FullName;
            def.Name = ActivtyFeedEventName;
            def.Type = eventType;
            def.SequenceNumber = 1000;
            def.Synchronization = SPEventReceiverSynchronization.Asynchronous;
            def.Update();
        }

        public static void UnregisterAllActivityFeedEvents(SPList list)
        {
            var listIdx = 0;
            while (listIdx < list.EventReceivers.Count)
            {
                if (list.EventReceivers[listIdx].Name == ActivtyFeedEventName)
                {
                    list.EventReceivers[listIdx].Delete();
                }
                else
                {
                    listIdx++;
                }

            }
        }

        public static bool IsEventRegistired(SPList list, Type eventReceiverClass, SPEventReceiverType eventType)
        {
            return
                list.EventReceivers.Cast<SPEventReceiverDefinition>().Any(
                    eventReceiver =>
                    eventReceiver.Name == ActivtyFeedEventName && eventReceiver.Type == eventType &&
                    eventReceiver.Class == eventReceiverClass.FullName);
        }

        public static void EnsureListRegistration(SPList list, Type eventReceiverClass, SPEventReceiverType eventType)
        {
            if (!IsEventRegistired(list, eventReceiverClass, eventType))
            {
                RegisterEventReceiver(list, eventReceiverClass, eventType);
            }
        }

        public static IEnumerable<ActiveListReceiver> GetAllListsWithActivityFeedEventRegistired(Type listItemReceiverType) 
        {
            var result = new List<ActiveListReceiver>();

            var webs = SPContext.Current.Site.AllWebs.OfType<SPWeb>();
            foreach (var web in webs)
            {
                var lists = web.Lists.OfType<SPList>();
                foreach (var list in lists)
                {
                    if (OpenWaves.ActivityFeed.Sharepoint.ListEventRegistration.IsEventRegistired(list, listItemReceiverType, SPEventReceiverType.ItemAdded))
                    {
                        var activeListReceiver = new ActiveListReceiver { List = list.Title, EventType = SPEventReceiverType.ItemAdded };
                        result.Add(activeListReceiver);
                    }
                    if (OpenWaves.ActivityFeed.Sharepoint.ListEventRegistration.IsEventRegistired(list, listItemReceiverType, SPEventReceiverType.ItemUpdated))
                    {
                        var activeListReceiver = new ActiveListReceiver { List = list.Title, EventType = SPEventReceiverType.ItemUpdated };
                        result.Add(activeListReceiver);
                    }
                }
            }

            return result;
        }
    }

    public class ActiveListReceiver
    {
        public string List { get; set; }
        public SPEventReceiverType EventType { get; set; }
    }
}